import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/page/home/api/api_home.dart';
import 'package:tender_flutter/page/home/item/credit_list_cell.dart';
import 'package:tender_flutter/page/home/model/credit_entity.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/load_state_layout.dart';

/// 失信查询结果列表
class CreditListPage extends StatefulWidget {
  /// 资讯类型
  /// 0 个人   1 企业
  final int type;

  final String name;

  final String code;

  const CreditListPage({Key key, this.type, this.name, this.code})
      : super(key: key);

  @override
  _CreditListPageState createState() => _CreditListPageState();
}

class _CreditListPageState extends State<CreditListPage> {
  Future<ApiResponse<CreditEntity>> _future;

  LoadState _loadState = LoadState.State_Loading;

  int page = AppConfig.page;

  int pageCount = AppConfig.pageCount;

  List<CreditList> _dataLists = [];

  /// 获取数据
  Future<ApiResponse<CreditEntity>> _getData() async {
    var response = await ApiHome.creditSearch<CreditEntity>(
        page: page, name: widget.name, code: widget.code);
    return response;
  }

  FutureBuilder<ApiResponse<CreditEntity>> _buildFutureBuilder() {
    return FutureBuilder(
        future: _future,
        builder: (BuildContext context,
            AsyncSnapshot<ApiResponse<CreditEntity>> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasData) {
              CreditEntity entity = snapshot.data.result;
              if (snapshot.data.status == Status.SUCCESS) {
                List temps = entity.content == null ? [] : entity.content;
                page == AppConfig.page
                    ? _dataLists = temps
                    : _dataLists += temps;
                if (_dataLists.isEmpty) {
                  _loadState = LoadState.State_Empty;
                } else {
                  _loadState = LoadState.State_Success;
                }
              } else {
                _loadState = LoadState.State_Error;
              }
            } else if (snapshot.hasError) {
              _loadState = LoadState.State_Error;
            }
          }

          return LoadStateLayout(
            bgColor: ThemeColors.colorWhite,
            state: _loadState,
            successWidget: _initList(),
          );
        });
  }

  @override
  void initState() {
    super.initState();
    _future = _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body: _buildFutureBuilder(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("查询结果", style: FonStyle.navTextStyle()),
      backgroundColor: ThemeColors.colorWhite,
    );
  }

  /// 列表
  Widget _initList() {
    return Padding(
        padding: EdgeInsets.only(top: ScreenAdapter.height(20)),
        child: ListView.separated(
          physics: const BouncingScrollPhysics(),
          itemCount: _dataLists.length,
          itemBuilder: (BuildContext context, int index) {
            CreditList model = _dataLists[index];
            return CreditListCell(data: model);
          },
          separatorBuilder: (BuildContext context, int index) {
            return Divider(
              color: ThemeColors.colorType49,
            );
          },
        ));
  }
}
